package cn.rongdao.jrkabao.util.growingio;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.mifengkong.frtools.app.FRApplication;
import com.mifengkong.frtools.glide.GlideApp;
import com.mifengkong.frtools.util.FRDeviceUtil;
import com.mifengkong.frtools.util.FRLog;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.frtools.util.FRString;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import cn.mifengkong.appcommon.util.ChannelUtil;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.dao.UserDao;


/**
 * Created by jiangyongxing on 2017/9/22.
 * 描述：
 */

public class BuriedUtil {

    public static final String KA_MAO_BURIED = "jihuo_jujuehuoqushebeihao_apply";

    public static Map<String, String> initBaseParmas(Context context) {
        Map<String, String> hashMap = new ArrayMap<>();
        hashMap.put("system", "Android");
        hashMap.put("source_id", ChannelUtil.getChannelName(context));
        hashMap.put("package_name", BuildConfig.APPLICATION_ID);
        hashMap.put("app_name", context.getResources().getString(R.string.app_name));
        hashMap.put("user_id", UserDao.getInstance().getUserId());
        hashMap.put("phone", UserDao.getInstance().getPhone());
        hashMap.put("dev_version", BuildConfig.VERSION_NAME);

        return hashMap;
    }

    /**
     * 需要传递全部的参数
     *
     * @param context
     * @param page_name 比如首页  身份证照片页面
     * @param event_key 当前事件的名称
     */
    public static void buringPointBase(final Context context, String page_name, String event_key, String tab, String banner, String button) {
        Map<String, String> hashMap = initBaseParmas(context);
        String eventName = TextUtils.concat(context.getString(R.string.product_code), event_key).toString();
        hashMap.put("page", FRString.valueOf(page_name));
        hashMap.put("event_key", FRString.valueOf(eventName));

        hashMap.put("tab", tab);
        hashMap.put("banner", FRString.valueOf(banner));
        hashMap.put("button", FRString.valueOf(button));

        hashMap.put("channel", FRSharedPreferences.getStringDefault(SPConstant.CHANNEL, ""));
        final String hashMapStr = hashMap2String(hashMap);
        try {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    if (context instanceof Activity && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
                        if (((Activity) context).isDestroyed()) {
                            return;
                        }
                    ImageView imageView = new ImageView(context);
                    imageView.setLayoutParams(new ViewGroup.LayoutParams(1, 1));
                    GlideApp.with(FRApplication.getInstance()).load(
                            new GlideUrl(TextUtils.concat(InfoConstant.BURING_POINT_URL, hashMapStr).toString(),
                                    new LazyHeaders.Builder().addHeader("Cookie", TextUtils.concat(InfoConstant.WEBVIEW_COOKIE_NAME, "=", FRDeviceUtil.getDeviceId(context)).toString()).build()))
                            .skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
                }
            });
        } catch (Exception e) {
            FRLog.error("埋点崩溃---" + Log.getStackTraceString(e));
            e.printStackTrace();
        }
    }

    private static String hashMap2String(Map<String, String> hashMap) {
        StringBuffer sb = new StringBuffer();
        sb.append("?");
        for (String key : hashMap.keySet()) {
            try {
                String value = hashMap.get(key);
                sb.append(TextUtils.concat(key, "=", URLEncoder.encode(value, "UTF-8"), "&").toString());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return sb.toString().trim().substring(0, sb.toString().trim().length() - 1);
    }


    /**
     * 新速贷_激活_用户拒绝授权手机设备号
     * <p>
     * 新速贷_激活_用户拒绝授权手机设备号
     *
     * @param context
     */
    public static void buringPoint_36(Context context) {
        if (FRString.isEmpty(FRSharedPreferences.getStringNotClear(KA_MAO_BURIED, ""))) {
            FRSharedPreferences.setStringNotClear(KA_MAO_BURIED, KA_MAO_BURIED);
            buringPointBase(context, "jihuo", KA_MAO_BURIED, "", "", "");
        }
    }

    public static void buring_shouye_click(Context context, String event_key, String banner, String button) {
        buringPointBase(context, "首页", event_key, "", banner, button);
    }

    //点击首页tab
    public static void kamao_tab_click_apply(Context context, String tab) {
        buringPointBase(context, "shouye", "tab_click_apply", tab, "", "");
    }

    //点击首页消息
    public static void kamao_shouye_fenleiye_apply(Context context) {
        buring_shouye_click(context, "shouye_fenleiye_apply", "", "首页_消息");
    }

    //点击首页取现
    public static void kamao_shouye_quxian_apply(Context context) {
        buring_shouye_click(context, "shouye_quxian_apply", "", "首页_信用卡取现");
    }

    //点击首页banner
    public static void kamao_shouye_banner_apply(Context context, String banner) {
        buring_shouye_click(context, "shouye_banner_apply", banner, "");
    }

    //点击首页办理会员广告
    public static void kamao_shouye_banlihuiyuan_apply(Context context) {
        buring_shouye_click(context, "shouye_banlihuiyuan_apply", "", "首页_办理会员");
    }

    //点击取现页面绑定信用卡
    public static void kamao_quxian_anniu_apply(Context context) {
        buringPointBase(context, "取现", "quxian_anniu_apply", "", "", "取现_绑定信用卡");
    }

    //点击实名页面上传身份证照片
    public static void kamao_shenfenyanzheng_anniu_apply(Context context, int index) {
        String button;
        if (index == 0) {
            button = "身份验证_上传身份证正面";
        } else if (index == 1) {
            button = "身份验证_上传身份证反面";
        } else {
            button = "身份验证_上传身份证手持";
        }
        buringPointBase(context, "身份验证", "shenfenyanzheng_anniu_apply", "", "", button);
    }

    //点击实名页面按钮
    public static void kamao_shiming_anniu_apply(Context context, boolean isName) {
        buringPointBase(context, "实名", "shiming_anniu_apply", "", "", isName ? "实名_更换身份证名字" : "实名_更换身份证号码");
    }

    //点击绑卡页面按钮
    public static void kamao_bangka_anniu_apply(Context context, int index) {
        String button;
        if (index == 0) {
            button = "绑卡_绑定银行卡";
        } else if (index == 1) {
            button = "绑卡_绑定手机号";
        } else {
            button = "绑卡_更换银行名称";
        }
        buringPointBase(context, "绑卡", "bangka_anniu_apply", "", "", button);
    }

    //点击支付页面按钮
    public static void kamao_zhifu_anniu_apply(Context context, int index) {
        String button;
        if (index == 0) {
            button = "支付页_CVN2";
        } else if (index == 1) {
            button = "支付页_有效期";
        } else if (index == 2) {
            button = "支付页_验证码";
        } else {
            button = "支付页_确认和支付";
        }
        buringPointBase(context, "支付页", "zhifu_anniu_apply", "", "", button);
    }

    //点击我的页面按钮
    public static void kamao_wode_anniu_apply(Context context, int index) {
        String button;
        if (index == 0) {
            button = "我的_退换码";
        } else if (index == 1) {
            button = "我的_邀请好友";
        } else if (index == 2) {
            button = "我的_帮助中心";
        } else if (index == 3) {
            button = "我的_取现记录";
        } else if (index == 4) {
            button = "我的_我的卡包";
        } else {
            button = "我的_我的贷款";
        }
        buringPointBase(context, "我的", "wode_anniu_apply", "", "", button);
    }

    //点击会员页面按钮
    public static void kamao_huiyuan_anniu_apply(Context context, int index) {
        String button;
        if (index == 0) {
            button = "会员_30天会员";
        } else if (index == 1) {
            button = "会员_90天会员";
        } else if (index == 2) {
            button = "会员_365天会员";
        } else {
            button = "会员_确认支付";
        }
        buringPointBase(context, "会员", "huiyuan_anniu_apply", "", "", button);
    }

    //浏览页面
    public static void kamao_yemian_jiazaiwancheng_apply(Context context, int index) {
        String pageName;
        if (index == 0) {
            pageName = "首页";
        } else if (index == 1) {
            pageName = "取现页面";
        } else if (index == 2) {
            pageName = "身份验证";
        } else if (index == 3) {
            pageName = "实名页面";
        } else if (index == 4) {
            pageName = "我的页面";
        } else if (index == 5) {
            pageName = "支付页面";
        } else {
            pageName = "会员页面";
        }
        buringPointBase(context, pageName, "yemian_jiazaiwancheng_apply", "", "", "");
    }

}